package com.android.assistivetouch;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private TextView mStateTv;
    private ToggleButton mStateTb;

    private IAssistiveTouchInterface mService;

    private boolean mEnableUnbind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mStateTv = findViewById(R.id.state);
        mStateTb = findViewById(R.id.on_off);

        mStateTb.setOnCheckedChangeListener(this);

        bindAssistiveTouchService();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateAssistiveTouchState();
    }

    @Override
    protected void onDestroy() {
        unbindAssistiveTouchService();
        super.onDestroy();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        boolean isOn = false;
        if (mService != null) {
            try {
                mService.setAssitiveTouchState(isChecked);
                isOn = isChecked;
            } catch (RemoteException e) {
                Log.d(this, "onCheckedChanged=>error: ", e);
            }
        }
        if (isOn) {
            mStateTb.setChecked(true);
            mStateTv.setText("辅助触控功能已打开");
        } else {
            mStateTb.setChecked(false);
            mStateTv.setText("辅助触控功能已关闭");
        }
    }

    private void updateAssistiveTouchState() {
        boolean isOn = false;
        if (mService != null) {
            try {
                isOn = mService.getAssitiveTouchState();
            } catch (RemoteException e) {
                Log.d(this, "updateAssistiveTouchState=>error: ", e);
            }
        }
        if (isOn) {
            mStateTb.setChecked(true);
            mStateTv.setText("辅助触控功能已打开");
        } else {
            mStateTb.setChecked(false);
            mStateTv.setText("辅助触控功能已关闭");
        }
    }

    private void bindAssistiveTouchService() {
        mEnableUnbind = false;
        Intent service = new Intent(this, AssistiveTouchService.class);
        bindService(service, mConncection, Service.BIND_AUTO_CREATE);
    }

    private void unbindAssistiveTouchService() {
        mEnableUnbind = true;
        unbindService(mConncection);
    }

    private ServiceConnection mConncection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IAssistiveTouchInterface.Stub.asInterface(service);
            updateAssistiveTouchState();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (!mEnableUnbind) {
                bindAssistiveTouchService();
            }
        }
    };


}